package jee.boot.entity.bpm;

import com.fasterxml.jackson.annotation.JsonIgnore;
import jee.boot.common.annotation.Table;
import jee.boot.common.annotation.Transient;
import jee.boot.common.basic.DataEntity;
import jee.boot.common.utils.JsonMapper;
import jee.boot.common.utils.StringUtils;

import java.util.Map;

/**
 * 业务对象
 */
@Table(name = "bpm_business_object")
public class BusinessObject extends DataEntity<BusinessObject> {
    /**
     * 别名
     */
    private String alias;
    /**
     * 名字
     */
    private String name;
    /**
     * 描述
     */
    private String description;
    /**
     * relation字段用来持久化入库的字符串字段
     */
    private String relationJson;
    /**
     * 关系
     */
    @Transient
    private BusTableRel relation;

    @Transient
    private Map<String,Object> relationMap;

    public BusinessObject() {
    }

    public BusinessObject(String id) {
        super(id);
    }

    public String getAlias() {
        return alias;
    }

    public void setAlias(String alias) {
        this.alias = alias;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public String getRelationJson() {
        return relationJson;
    }

    public void setRelationJson(String relationJson) {
        this.relationJson = relationJson;
    }

    @JsonIgnore
    public BusTableRel getRelation() {
        if (relation == null && StringUtils.isNotBlank(relationJson)) {
            relation = JsonMapper.fromJsonString(relationJson, BusTableRel.class);
        }
        return relation;
    }

    public void setRelation(BusTableRel relation) {
        this.relation = relation;
        if (relation != null) {
            relationJson = JsonMapper.toJsonString(relation);
        }
    }
    @JsonIgnore
    public Map<String, Object> getRelationMap() {
        if (relationMap == null && StringUtils.isNotBlank(relationJson)) {
            relationMap = JsonMapper.fromJsonString(relationJson, Map.class);
        }
        return relationMap;
    }

    public void configRelationMap(Map<String, Object> relationMap) {
        this.relationMap = relationMap;
        if (relationMap != null) {
            relationJson = JsonMapper.toJsonString(relationMap);
            relation = JsonMapper.fromJsonString(relationJson, BusTableRel.class);
        }
    }

    @Override
    public void preInsert() {
        super.preInsert();
    }
}
